# coding=utf-8

"""
    Created by Kalyter on 2017-11-15.
"""

import os
import re


def read(filename):
    open_file = open(filename, 'r')
    try:
        content = open_file.read()
        content = content.replace('\n', '').strip()
    finally:
        open_file.close()
    content = re.match(r'.*<body>(.*)</body>', content).group(1)
    return content


def read_path(path):
    file_list = os.listdir(path)
    ignore = ['TPP.xml', 'TPPspanish.xml']
    filter(lambda x: x not in ignore, file_list)
    for file in file_list:
        yield path + '/' + file


def split_string(content, step):
    result = set()
    for i in range(step, len(content) + 1):
        result.add(content[i - step: i])
    return result


def jaccard(a, b, step):
    a = split_string(a, step)
    b = split_string(b, step)
    t = [v for v in a if v in b]
    c = set()
    [c.add(i) for i in t]
    return float(len(c))/(len(a)+len(b)-len(c))


def compare_by_jaccard(x, y, step=5):
    return jaccard(x, y, step)


def main(step):
    base = read('./xml/TPP.xml')
    for filename in read_path('./xml'):
        body = read(filename)
        result = compare_by_jaccard(base, body, step)
        print('%s   相似度   %.04f' % (filename, result))


if __name__ == '__main__':
    main(5)
